package cn.iocoder.yudao.module.contract.job;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.contract.controller.admin.client.vo.ClientContractUploadCycleVO;
import cn.iocoder.yudao.module.contract.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.orderContract.OrderContractDO;
import cn.iocoder.yudao.module.contract.service.client.ClientService;
import cn.iocoder.yudao.module.contract.service.order.OrderService;
import cn.iocoder.yudao.module.contract.service.orderContract.OrderContractService;
import cn.iocoder.yudao.module.contract.webservice.ContractWebService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @Author warrior
 **/
@Component
@Slf4j
public class ClientContractUploadJob implements JobHandler {

    @Autowired
    private ClientService clientService;

    @Autowired
    private OrderContractService orderContractService;

    @Autowired
    private OrderService orderService;

    @Resource
    private ContractWebService contractWebService;

    @Override
    public String execute(String param) throws Exception {
        //获取客户合同上传周期信息
        List<ClientContractUploadCycleVO> clientContractUploadCycleVOS = clientService.listOpenCycleClientContractUploadCycle();
        Map<String, Integer> clientUploadCycleMap = clientContractUploadCycleVOS.stream().collect(Collectors.toMap(ClientContractUploadCycleVO::getClientCode, ClientContractUploadCycleVO::getContractUploadCycle));
        //获取未上传合同附件的合同
        List<OrderContractDO> orderContractDOS = orderContractService.listUnUploadContractFile();
        orderContractDOS.stream().forEach(orderContractDO -> {
            //获取合同订单
            OrderDO orderDO = orderService.getOrder(orderContractDO.getOrderId());
            if (orderDO == null) {
                log.warn("合同订单不存在，合同Id：{}", orderContractDO.getId());
            } else {
                Integer uploadCycle = clientUploadCycleMap.get(orderDO.getKunnr());
                if(uploadCycle != null){
                    // 获取当前日期时间
                    LocalDateTime now = LocalDateTime.now();
                    LocalDateTime createTime = orderDO.getCreateTime();
                    long daysBetween = ChronoUnit.DAYS.between(createTime.toLocalDate(), now.toLocalDate());
                    if (uploadCycle <= daysBetween) {
                        boolean success = contractWebService.pushContractFreeze(orderDO.getVbeln(), orderContractDO);
                        orderContractService.updateById(orderContractDO);
                        if (success) {
                            orderContractService.orderContractFreeze(orderContractDO.getId());
                        }
                    }
                }
            }
        });
        return null;
    }
}
